<?php

namespace App\Store\Models\Wxapp\Gift;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;

class WxappGiftAllot extends Model
{
    protected $table = 'site_wxapp_gift_allot';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['store_info','target_store_info'])->where('is_del', 1)->where('cp_id',$cp_id)->orderBy('create_at', 'desc');

        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                // $query->whereIn('store_id',$store_ids);
                $query->where(function ($q) use ($store_ids){
                    $q->whereIn('store_id', $store_ids)->orWhereIn('target_store_id', $store_ids);
                });
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where(function ($q) use ($store_ids){
                    $q->where('store_id', intval($store_ids))->orWhere('target_store_id', intval($store_ids));
                });
            }
        }


        $store_id = intval($request->store_id);
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }

        $target_store_id = intval($request->target_store_id);
        if ($target_store_id>0) {
            $query->where('target_store_id', $target_store_id);
        }

        $audit_status = intval($request->audit_status);
        if ($audit_status>0) {
            $query->where('audit_status', $audit_status);
        }



        $gift_name = $request->gift_name;
        if ($gift_name != '') {
            $query->where('gift_name', 'like', '%' . $gift_name . '%');
        }
        
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }
        
        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    public function store_info(){
        return $this->hasOne(SiteStore::class,'id','store_id')->select(['id','store_name']);
    }


    public function target_store_info(){
        return $this->hasOne(SiteStore::class,'id','target_store_id')->select(['id','store_name']);
    }
}